package BuildTrainSet.Initial;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

/**
 *
 * @author phamthanh
 */
public final class DetectGroup {
//  public ArrayList<String> greekAr=new ArrayList<String>();
    public DetectGroup(String inF, String ouF)throws XMLStreamException, FileNotFoundException, IOException{
//        GreekNumber();
        
        long startTime = System.currentTimeMillis();
        int un=0;
        int mn=0;
        int sn=0;

        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        System.out.println("class DetectGroup, method DetectGroup");

        FileOutputStream fos = new FileOutputStream(ouF);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
        XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(osw);
        xmlStreamWriter.writeStartElement("root");
        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {

                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("record")) {
                        String RecId = xmlStreamReader.getAttributeValue(0);
                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeAttribute("RecId", RecId);

                    }

                    if (localName.equals("URL")) {
                        String url = xmlStreamReader.getElementText();

                        xmlStreamWriter.writeStartElement(localName);
                        xmlStreamWriter.writeCharacters(url);
                        xmlStreamWriter.writeEndElement();

                    }
                    if (localName.equals("Infomation")) {
                        xmlStreamWriter.writeStartElement(localName);
                    }

                    if (localName.equals("W")) {
                        String nsw = xmlStreamReader.getElementText();
                        
                        xmlStreamWriter.writeStartElement(localName);
                        
//                        if(isURL(nsw)){
//                            xmlStreamWriter.writeAttribute("NSW", "URL");
//
//                        }
//                        
//                        if(isMoney(nsw)){
//                            xmlStreamWriter.writeAttribute("NSW", "MONEY");
//
//                        }
//                        if(isSeqn(nsw)){
//                            xmlStreamWriter.writeAttribute("NSW", "SEQN");
//
//                        }
                        String tag=detectTag(nsw);
                        xmlStreamWriter.writeAttribute("NSW", tag);
                        xmlStreamWriter.writeCharacters(nsw);
                        xmlStreamWriter.writeEndElement();
                        if(tag.equals("URL")){
                            un++;
                        }else{
                            if(tag.equals("MONEY")){
                                mn++;
                            }else{
                                if(tag.equals("SEQN")){
                                    sn++;
                                }
                            }
                        }

                    }
                    break;
                case XMLStreamConstants.END_ELEMENT:
                    String endLocalName = xmlStreamReader.getLocalName();
                    if (endLocalName.equals("record")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    if (endLocalName.equals("Infomation")) {
                        xmlStreamWriter.writeEndElement();

                    }
                    break;
                case XMLStreamConstants.CHARACTERS:
                    String character = xmlStreamReader.getText();
                    xmlStreamWriter.writeCharacters(character);
                    break;

                default:
                    break;
            }
        }
        xmlStreamWriter.writeEndElement();

        xmlStreamReader.close();
        xmlStreamWriter.close();
        fis.close();
        fos.close();
        long endTime = System.currentTimeMillis();
        long time=endTime-startTime;
        System.out.println("    +url: "+un);
        System.out.println("    +money: "+mn);
        System.out.println("    +seqn: "+sn);       
    }
    
    public boolean isURL(String nsw){
        Pattern URL=Pattern.compile("(^((http://)|(@)|(www))|(((.net)|(.vn)|(.org)|(.com))$))");
        Matcher m=URL.matcher(nsw);
        return m.find();       
    }
    
    public boolean isMoney(String nsw){
        Pattern MONEY1=Pattern.compile("(\\d[\\d\\.,\\p{Pd} ]*)?\\d[ ]?((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(đ\\b)|(đồng\\b)|(\\$)|(\\p{Lu}+\\$))");
        Pattern MONEY2=Pattern.compile("((AUD)|(SGD)|(USD)|(VNĐ)|(Đ\\b)|(đ\\b)|(\\$)|(\\p{Lu}+\\$))[ ]?(\\d[\\d\\.,\\p{Pd} ]*)?\\d");
        Pattern MONEY3=Pattern.compile("[\\d][\\d\\. ,]*000/[^ ]+");
        Matcher m=MONEY1.matcher(nsw);
        if( m.find()) return true;
        m=MONEY2.matcher(nsw);
        if( m.find()) return true;
        m=MONEY3.matcher(nsw);
        if( m.find()) return true;
        
        return false;
    }
    
    public boolean isSeqn(String nsw){
        Pattern NAME=Pattern.compile("(\\b(\\p{Lu}\\.)+(\\p{Lu}\\b))");
        Matcher m=NAME.matcher(nsw);
        return m.find();
    }
    
    public boolean isPunc(String nsw){
        Pattern PUNC=Pattern.compile("(\\b\\(\\b)|(\\b\\)\\b)|(\\b\"\\b)|(\\b\'\\b)|(\\b...\\b)|");
        Matcher m=PUNC.matcher(nsw);
        return m.find();
    }
    
//    public boolean isGreekNumber(String nsw){
//        if(greekAr.contains(nsw)){
//            return true;
//        }
//        return false;
//    }
//     public void GreekNumber(){
//        greekAr.add("I");
//        greekAr.add("II");
//        greekAr.add("III");
//        greekAr.add("IV");
//        greekAr.add("V");
//        greekAr.add("VI");
//        greekAr.add("VII");
//        greekAr.add("VIII");
//        greekAr.add("IX");
//        greekAr.add("X");
//        greekAr.add("XI");
//    }
            
    public String detectTag(String nsw){
//         if(isGreekNumber(nsw)){
//            return "GREEK";
//        }
        if(isURL(nsw)){
            return "URL";
        }
        if(isMoney(nsw)){
            return "MONEY";
        }

        if(isSeqn(nsw)){
            return "SEQN";
        }
        return "";

    }

  
}
